{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-22T09:01:17.882503Z",
     "start_time": "2024-12-22T09:01:17.840505Z"
    }
   },
   "source": [
    "import torch\n",
    "import d2l\n",
    "print(torch.__version__)\n",
    "print(d2l.__version__)\n",
    "torch.cuda.is_available()"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.4.1\n",
      "1.0.3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:02:55.463476Z",
     "start_time": "2024-12-22T09:02:55.449471Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 张量 数值组成的数组\n",
    "x = torch.arange(12)\n",
    "x"
   ],
   "id": "e871991c3b41396d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:04:33.130246Z",
     "start_time": "2024-12-22T09:04:33.119827Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(x.shape)\n",
    "print(x.numel())"
   ],
   "id": "e3e6efc7716c7d3d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([12])\n",
      "12\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:04:52.524800Z",
     "start_time": "2024-12-22T09:04:52.510783Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X = x.reshape(3, 4)\n",
    "X"
   ],
   "id": "4de0bc950491d8bc",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:05:39.859004Z",
     "start_time": "2024-12-22T09:05:39.847011Z"
    }
   },
   "cell_type": "code",
   "source": "torch.ones(2, 3, 4)",
   "id": "55240cc6f3637a9e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.]],\n",
       "\n",
       "        [[1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.]]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:05:51.852622Z",
     "start_time": "2024-12-22T09:05:51.839623Z"
    }
   },
   "cell_type": "code",
   "source": "torch.zeros(2, 3, 4)",
   "id": "2a4b76954cfa4b68",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]],\n",
       "\n",
       "        [[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:08:25.603266Z",
     "start_time": "2024-12-22T09:08:25.591268Z"
    }
   },
   "cell_type": "code",
   "source": [
    "y = torch.tensor([[1, 2, 3], [2, 1, 3], [3, 2, 1]])\n",
    "print(y)\n",
    "y.shape"
   ],
   "id": "1f0de37a220292c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1, 2, 3],\n",
      "        [2, 1, 3],\n",
      "        [3, 2, 1]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 3])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:11:48.552397Z",
     "start_time": "2024-12-22T09:11:48.541344Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x = torch.arange(9, dtype=torch.float32).reshape(3, 3)\n",
    "y = torch.tensor([[1, 2, 3], [2, 1, 3], [3, 2, 1]])\n",
    "torch.cat((x, y), dim = 0), torch.cat((x, y), dim = 1)"
   ],
   "id": "1141313df2d0f925",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[0., 1., 2.],\n",
       "         [3., 4., 5.],\n",
       "         [6., 7., 8.],\n",
       "         [1., 2., 3.],\n",
       "         [2., 1., 3.],\n",
       "         [3., 2., 1.]]),\n",
       " tensor([[0., 1., 2., 1., 2., 3.],\n",
       "         [3., 4., 5., 2., 1., 3.],\n",
       "         [6., 7., 8., 3., 2., 1.]]))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:13:03.730663Z",
     "start_time": "2024-12-22T09:13:03.723154Z"
    }
   },
   "cell_type": "code",
   "source": "x == y",
   "id": "8c75732b5b42dc18",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[False, False, False],\n",
       "        [False, False, False],\n",
       "        [False, False, False]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:13:28.847600Z",
     "start_time": "2024-12-22T09:13:28.835467Z"
    }
   },
   "cell_type": "code",
   "source": "x.sum(), y.sum()",
   "id": "b3eee1c18b944d1e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor(36.), tensor(18))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:15:50.707610Z",
     "start_time": "2024-12-22T09:15:50.691139Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 广播机制\n",
    "a = torch.arange(3).reshape(1, 3)\n",
    "b = torch.arange(3).reshape(3, 1)\n",
    "a + b"
   ],
   "id": "de656d7c5b9dcd8a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1, 2],\n",
       "        [1, 2, 3],\n",
       "        [2, 3, 4]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:16:31.346056Z",
     "start_time": "2024-12-22T09:16:31.334530Z"
    }
   },
   "cell_type": "code",
   "source": "x[-1], x[1:3]",
   "id": "284fc6254a064e95",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([6., 7., 8.]),\n",
       " tensor([[3., 4., 5.],\n",
       "         [6., 7., 8.]]))"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:17:12.916300Z",
     "start_time": "2024-12-22T09:17:12.905303Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X[1, 1] = 1\n",
    "print(X)\n",
    "X[1, 1] = 999\n",
    "print(X)"
   ],
   "id": "f2bcf7dfe915fb30",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0,  1,  2,  3],\n",
      "        [ 4,  1,  6,  7],\n",
      "        [ 8,  9, 10, 11]])\n",
      "tensor([[  0,   1,   2,   3],\n",
      "        [  4, 999,   6,   7],\n",
      "        [  8,   9,  10,  11]])\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:17:40.490779Z",
     "start_time": "2024-12-22T09:17:40.476719Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X[0:2, :] = 12\n",
    "X"
   ],
   "id": "a3fac94c44b01312",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[12, 12, 12, 12],\n",
       "        [12, 12, 12, 12],\n",
       "        [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:19:33.844547Z",
     "start_time": "2024-12-22T09:19:33.832549Z"
    }
   },
   "cell_type": "code",
   "source": [
    "z = torch.zeros_like(y)\n",
    "before = id(z)\n",
    "z[:] = x + y\n",
    "after = id(z)\n",
    "print(before, z, after)"
   ],
   "id": "1a1e799455c0f07f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2502108177568 tensor([[1, 3, 5],\n",
      "        [5, 5, 8],\n",
      "        [9, 9, 9]]) 2502108177568\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:20:51.144922Z",
     "start_time": "2024-12-22T09:20:51.132924Z"
    }
   },
   "cell_type": "code",
   "source": [
    "A = x.numpy()\n",
    "B = torch.tensor(A)\n",
    "type(A), type(B)"
   ],
   "id": "37cf7a86292fc433",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(numpy.ndarray, torch.Tensor)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-22T09:21:31.184190Z",
     "start_time": "2024-12-22T09:21:31.171213Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = torch.tensor([1])\n",
    "a.item(), int(a)"
   ],
   "id": "a2418f51168e5c26",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 1)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "7fc46cda6b1a0539"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
